package com.wclass.controller.sys;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseAppUser;
import com.wclass.entity.CouresOrder;
import com.wclass.entity.CourseInfo;
import com.wclass.interceptor.BaseContextHandler;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.ICouresOrderService;
import com.wclass.service.ICourseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程订单 后台控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-01-07
 */
@RestController
@RequestMapping("/sysCouresOrder")
public class SysCouresOrderController {
    @Autowired
    private ICouresOrderService iCouresOrderService;
    @Autowired
    private ICourseInfoService iCourseInfoService;
    @Autowired
    private IBaseAppUserService iBaseAppUserService;

    @IgnoreAuth
    @ApiOperation("获取订单分页")
    @RequestMapping(value = "/getOrders", method = RequestMethod.GET)
    public ReturnObject<List<CouresOrder>> getPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                   @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                   @ApiParam("支付状态(0：等待支付，1：支付成功，2：退款中，3：退款成功)") Integer payState) {
        EntityWrapper<CouresOrder> ew = new EntityWrapper<>();
        ew.orderBy("crt_time", false);
        if (payState != null) {
            ew.eq("pay_state", payState);
        }
        Page<CouresOrder> obj = iCouresOrderService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @IgnoreAuth
    @ApiOperation("获取订单数量")
    @RequestMapping(value = "/getOrdersNotPage", method = RequestMethod.GET)
    public HashMap<String, Object> getOrdersNotPage() {
        EntityWrapper<CouresOrder> ew = new EntityWrapper<>();
        ew.orderBy("crt_time", false);
        ew.eq("pay_state", 1);
        List<CouresOrder> courseOrders=iCouresOrderService.selectList(ew);
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","获取成功");
        HashMap<String,Object> data=new HashMap<String,Object>();
        data.put("orderNum",courseOrders.size());
        data.put("orderMoney","");
        map.put("data",data);
        return map;
    }


    @ApiOperation("订单详情")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public ReturnObject<CouresOrder> info(@ApiParam("订单id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", iCouresOrderService.selectById(id));
    }

    @ApiOperation("删除订单")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject del(@ApiParam("订单id") @RequestParam Long id) {
        CouresOrder couresOrder = iCouresOrderService.selectById(id);
        if (couresOrder.getPayState() == 1 || couresOrder.getPayState() == 2) {
            return new ReturnObject<>(1, "支付成功及退款中的订单无法删除");
        }
        iCouresOrderService.deleteById(id);
        return new ReturnObject<>(0, "删除成功");
    }


}

